package com.helloblog.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DateSourceUtils {
	private static DataSource ds;
	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
	static{
		try{
			ds = new ComboPooledDataSource();
		}catch(Exception e){
			throw new ExceptionInInitializerError(e);
		}
	}
	public static DataSource getDataSource(){
		return ds;
	}
	
	public static Connection getConnection(){
		Connection con = null;
		con = tl.get();
		if(con==null){
			try {
				con = getDataSource().getConnection();
				tl.set(con);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return con;		
	}
	public static void remove(){
		tl.remove();
	}
}	
